package eu.reply.android.flashm;

import java.util.ArrayList;

import com.google.android.gcm.GCMRegistrar;

import eu.reply.android.flashm.entities.Event;
import eu.reply.android.flashm.exceptions.BEServiceNetException;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class MainActivity extends ListActivity implements OnItemClickListener {

	public final static String EXTRA_EVENT_ID = "com.reply.android.flashm.EVENT.ID";
	public final static String EXTRA_EVENT_NAME = "com.reply.android.flashm.EVENT.NAME";
	public final static String EXTRA_EVENT_DATE = "com.reply.android.flashm.EVENT.DATE";
	public final static String EXTRA_EVENT_LOCATION = "com.reply.android.flashm.EVENT.LOCATION";
	public final static String EXTRA_EVENT_DESCRIPTION = "com.reply.android.flashm.EVENT.DESCRIPTION";
	public final static String EXTRA_EVENT_ATTENDEES = "com.reply.android.flashm.EVENT.ATTENDEES";
	public final static String EXTRA_EVENT_IMAGE_ID = "com.reply.android.flashm.EVENT.IMAGEID";
	
	public final static String TAG = "MainActivity";
	public final static String SENDER_ID = "608941147332";

	EventListAdapter eventListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		

		//GCMIntentService.displayNotifcation(this, "EventName", "EventDescription", "location");

		registerToGCM();

		eventListAdapter = new EventListAdapter(this, new ArrayList<Event>());
		setListAdapter(eventListAdapter);

		getListView().setOnItemClickListener(this);

		hidePanel();

		new RefreshEventsTask().execute();
	}

	private void registerToGCM() {
		Log.i(TAG, "Registerinig to GCM");
		
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
		  GCMRegistrar.register(this, SENDER_ID);
		} else {
			Log.w(TAG, "Already registered: "+regId);
			try {
				BEAPI.registerUser("Andrea", regId);
			} catch (BEServiceNetException e) {
				Log.e("GCMIntentService", "Error while sending registration ID: " +e.toString());
			}
		  
		}
		
	}

	public void onItemClick(AdapterView<?> adapter, View view, int position,
			long id) {
		Event event = (Event) adapter.getItemAtPosition(position);

		Intent intent = new Intent(this, EventDetailsActivity.class);
		intent.putExtra(EXTRA_EVENT_ID, event.getId());
		intent.putExtra(EXTRA_EVENT_NAME, event.getName());
		intent.putExtra(EXTRA_EVENT_DATE, event.getDate().toString());
		intent.putExtra(EXTRA_EVENT_DESCRIPTION, event.getDescription());
		intent.putExtra(EXTRA_EVENT_LOCATION, event.getLatLongt() );
		intent.putExtra(EXTRA_EVENT_IMAGE_ID, event.getImageResourceId());

		int att = event.getAttending().size();
		if (att == 0) {
			intent.putExtra(EXTRA_EVENT_ATTENDEES, "No attendees yet.");
		} else {
			intent.putExtra(EXTRA_EVENT_ATTENDEES, event.getAttending().size()
					+ " attendee(s)");
		}

		startActivity(intent);
	}

	public void onNothingSelected(AdapterView<?> arg0) {

	}

	private void showPanel(String pMessage, boolean pLoadingVisible) {
		this.findViewById(R.id.main_message_panel).setVisibility(View.VISIBLE);
		((TextView) this.findViewById(R.id.main_message_text))
				.setText(pMessage);
	}

	private void hidePanel() {
		this.findViewById(R.id.main_message_panel)
				.setVisibility(View.INVISIBLE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void viewOnMap(View view) {
		Intent intent = new Intent(this, ViewOnMap.class);
		// intent.putExtra(LOCATION, dummyLoc.getExtras());
		startActivity(intent);
	}

	private class RefreshEventsTask extends
			AsyncTask<Void, Void, ArrayList<Event>> {

		@Override
		protected ArrayList<Event> doInBackground(Void... params) {
			Log.d("MainActivity", "doInBackground");

			showPanel("Loading flash-mobs, please wait...", false);

			try {
				return BEAPI.getEventList();
			} catch (BEServiceNetException e) {
				e.printStackTrace();
				showPanel("Ooops! Something really bad has happened.", false);
				return null;
			}
		}

		@Override
		protected void onPostExecute(ArrayList<Event> result) {
			Log.d("MainActivity", "onPostExecute");

			if (result != null) {
				eventListAdapter.clear();
				eventListAdapter.addAll(result);

				hidePanel();
			} else {
				showPanel("No flash-mobs yet.", false);
			}
			
			
		}
	}
}
